<template>
  <div class="app-container">
    <div class="handle-content">
      <el-input v-if="this.$store.getters.role.store_id == 0" v-model="storeId" placeholder="请输入店铺id" style="width: 200px;margin-right: 10px;">
        
      </el-input>
      <el-date-picker
        v-model="time"
        type="datetimerange"
        start-placeholder="开始日期"
        end-placeholder="结束日期">
      </el-date-picker>
      <el-button slot="append" @click="search">查询</el-button>
      <el-button slot="append" type="primary" @click="dialogSure">发放</el-button>
      <span class="right">
        <el-button type="primary" @click="exportExcel">
          导出
        </el-button>
      </span>
    </div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>汇总信息</span>
      </div>
      <div class="text item">
        {{ '总金额：' + reward }}
        <br>
        <br>
        {{ '总'+ awardName + '：' + bd_index }}
        <br>
        <br>
        {{ awardName + '单位价值：' + person_get }}
        <br>
        <br>
        {{ '发放金额数：' + send_reward }}
      </div>
    </el-card>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row ref="listQuery.checkedUsers" @selection-change="handleSelectionChange">
      <el-table-column
      type="selection"
      width="55">
      </el-table-column>
      <el-table-column v-if="this.$store.getters.role.store_id == 0" label="店铺ID" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.store_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户ID" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="用户等级" align="center">
        <template slot-scope="scope">
          {{ scope.row.grade }}
        </template>
      </el-table-column>
      <el-table-column :label="awardName" align="center">
        <template slot-scope="scope">
          <span style="color: rgb(64, 158, 255);cursor:pointer;">{{ scope.row.bd_index }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应得金额" align="center">
        <template slot-scope="scope">
          <span style="color: rgb(64, 158, 255);cursor:pointer;">{{ scope.row.reward }}</span>
        </template>
      </el-table-column>
    </el-table>
    <dialog-model ref="dialog" @sure="bdMemSend" />
  </div>
</template>

<script>
/* eslint-disable */
import { bdMemList,bdMemSend } from '@/api/member'
import { Message } from 'element-ui'
import DialogModel from '@/components/Dialog'
import { getToken } from '@/utils/auth' // get token from cookie
import { parseTime } from '@/utils/index'
export default {
  components: { DialogModel },
  data() {
    return {
      list: [],
      listQuery: {
        storeId: 0,
        begin_at: '',
        end_at: '',
        checkAll:'false',
        checkedUsers:[]//选择发放的用户
      },
      storeId: '',
      reward: 0,
      bd_index: 0,
      person_get: 0,
      send_reward: 0,
      awardName: '报单指数',
      path:'',
      time:'',
      listLoading: false
    }
  },

  mounted() {
    this.getList()
  },

  methods: {
    getList() {
      this.listLoading = true
      bdMemList(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data.data
        this.reward = response.data.reward
        this.bd_index = response.data.bd_index
        this.person_get = response.data.person_get
        this.send_reward = response.data.send_reward
        this.awardName = response.data.awardName
        this.path = response.data.path
        this.listLoading = false
      })
    },
    dialogSure(){
      this.$refs.dialog.showDialog('您确定要发放虹包吗?')
    },
    bdMemSend() {
      this.listLoading = true
      this.listQuery.storeId = this.storeId
      bdMemSend(this.listQuery).then(response => {
        console.log(response)
        if(response.code == 20000){
          Message({
            message: response.data.info,
            type: 'success',
            duration: 2000
          })
        }
        // this.getList()
      })
    },
    search() {
      this.listQuery.storeId = this.storeId
      this.listQuery.begin_at = parseTime(this.time[0])
      this.listQuery.end_at = parseTime(this.time[1])
      console.log(this.storeId)
      console.log(this.listQuery)
      this.getList()
    },
    exportExcel() {
      const url = this.path + '?storeId=' + this.listQuery.storeId + '&begin_at=' + this.listQuery.begin_at + '&end_at=' + this.listQuery.end_at + '&token=' + getToken() + '&report=' + true

      location.href = url
    },
    handleSelectionChange(val) {
        this.listQuery.checkedUsers = val
        if(this.listQuery.checkedUsers.length == this.list.length){
            this.listQuery.checkAll = true
        }else{
            this.listQuery.checkAll = false
        }
        console.log(this.listQuery.checkedUsers)
        console.log(this.listQuery.checkAll)
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.app-container{
    .handle-content{
        margin-bottom: 20px;
        .right{
            float: right;
        }
    }
}
</style>
<style>
.el-table .warning-row {
}

.el-table .success-row {
}
</style>
